﻿using System;
using System.Net;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightPlayground.Net
{
    public static class SocketUtilities
    {
        /// <summary>
        /// Toes the ASCII string.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        public static string ToASCIIString(byte[] data)
        {
            return new string(
                (from b in data
                 select (char)b).ToArray());
        }

        /// <summary>
        /// Toes the ASCII bytes.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        public static byte [] ToASCIIBytes(string data)
        {
            return (from c in data.ToCharArray()
                    select (byte) c).ToArray();
        }

        /// <summary>
        /// Toes the u16.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        public static int ToU16(byte[] data)
        {
            return SocketUtilities.ToU16(data, 0);
        }

        /// <summary>
        /// Toes the u16.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="index">The index.</param>
        /// <returns></returns>
        public static int ToU16(byte[] data, int index)
        {
            return (int)data[index] << 8 | (int)data[index + 1];
        }

        /// <summary>
        /// Toes the u32.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        public static long ToU32(byte[] data)
        {
            return SocketUtilities.ToU32(data, 0);
        }

        /// <summary>
        /// Toes the u32.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        public static long ToU32(byte[] data, int index)
        {
            return (long)data[index] << 24 | (long)data[index + 1] << 16 | (long)data[index + 2] << 8 | (long)data[index + 3];
        }


    }
}
